<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head th:include="blog/index/include_blog::header"></head>
<body>
	<nav th:include="blog/index/include_blog::nav"  class="navbar navbar-default navbar-custom navbar-fixed-top" ></nav>
	<!-- Page Header -->
	<header class="intro-header"
		style="background-image: url('/img/blog/post-bg.jpg')">
		<div class="container">
			<div class="row">
				<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
					<div class="post-heading">
						<input type="hidden" id="created" th:value="${author.userId}">
						<h1><a th:href="'/blog/open/author/'+${author.userId}"
								th:text="${author.name}"></a>&nbsp;</h1>
						<span class="meta">文章数：&nbsp;<span id="number"></span>
						</span>
					</div>
				</div>
			</div>
		</div>
	</header>
	<!-- Main Content -->
	<div class="container">
		<div class="row">
			<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
				<div id="incomeNum"></div>
				<div class="clearfix">
					<p id="flagLoaded" style="display: none; text-align: center;">已全部加载</p>
				</div>
			</div>
		</div>
	</div>
</body>
<div th:include="blog/index/include_blog::footer"></div>
<script type="text/javascript">
	var limit = 10;
	var currentPage = 0;
	var total;
	var search;
	$(function () {
		$("#top-search").empty();
		bindList(0, $("#top-search").val(), $("#created").val(), true);
	});
	$("#top-search").keydown(function (e) {
		if (e.keyCode == 13) {
			bindList(0, $("#top-search").val(), $("#created").val(), true);
		}
	});
	function nextPage() {
		bindList(currentPage * limit, $("#top-search").val(), $("#created").val(), false)
	}

	function bindList(offset, search, author, bind) {
		$.ajax({
			url: '/blog/open/list?limit=10&offset=' + offset + '&title=' + search + '&created=' + author,
			method: 'get',
			async: false,
			dataType: 'json',
			success: function (data) {
				var rows = data.rows;
				total = data.total;
				$("#number").text(total);
				var htmlText = "";
				for (i = 0; i < rows.length; i++) {
					htmlText += '<div class="post-preview">';
					htmlText += '<a href="/blog/open/post/' + rows[i].cid + '">';
					htmlText += '<h2 class="post-title">';
					htmlText += rows[i].title;
					htmlText += '</h2>';
					htmlText += '</a>';
					htmlText += '<p class="post-meta">作者：<a href="/blog/open/author/'+ rows[i].created +'">'
							+ rows[i].author
							+ '</a> &nbsp;&nbsp; '
							+ rows[i].gtmModified + '</p>';
					htmlText += '</div>';
					htmlText += '<hr>';
				}
				if (bind) {
					$("#incomeNum").empty();
					currentPage = 0;
				}
				$("#incomeNum").append(htmlText);
				currentPage++;
				if (total <= currentPage * limit) {
					document.getElementById("flagLoaded").style.display = "block";
				} else {
					document.getElementById("flagLoaded").style.display = "none";
				}
			}
		});
	}
	$(function () {
		/**
		 *绑定滚动事件
		 */
		$(window).scroll(function (e) {
			if ($(window).scrollTop() + $(window).height() >= $(document).height() - 200) {
				if (total > currentPage * limit) {
					nextPage();
				}
			}
		});
	});
</script>
</body>
</html>
